package com.example.project22_editingmediaplayer;

import java.util.ArrayList;

import com.example.minimusicdemo.Main_play;
import com.example.project22_editingmediaplayer.action.AudioUltility;
import com.example.project22_editingmediaplayer.adapter.RowAlbum;
import com.example.project22_editingmediaplayer.objects.AlBumObject;
import com.example.project22_editingmediaplayer.objects.ArtisObject;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class LayoutAlbums extends Fragment {
	Context myContext;
	ListView listview_album;
	RowAlbum albumAdapter;
	ArrayList<AlBumObject> listAlbum;
	public static ArrayList<SongObject> listAlbumsong;
	AudioUltility audioUltility;

	public static Fragment newInstance(Context context) {
		LayoutAlbums f = new LayoutAlbums();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_albums,
				null);
		myContext = container.getContext();
		return root;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		listview_album = (ListView) getActivity().findViewById(R.id.list_album);
		listAlbum = new ArrayList<AlBumObject>();
		audioUltility = new AudioUltility(myContext);
		albumAdapter = new RowAlbum(listAlbum, myContext);
		listview_album.setAdapter(albumAdapter);

		new LoadAlbum().execute();

		listview_album.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				AlBumObject albumObject = albumAdapter.getItem(arg2);
				int id_album = albumObject.get_id();
				listAlbumsong = audioUltility.getAllSongByAlbum(id_album);

				Intent intent_artis = new Intent(getActivity(),
						AlbumListDetail.class);

				getActivity().startActivity(intent_artis);

			}
		});

		registerForContextMenu(listview_album);

	}

	private class LoadAlbum extends
			AsyncTask<Void, Void, ArrayList<AlBumObject>> {

		@Override
		protected ArrayList<AlBumObject> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return audioUltility.getAllAlbum();

		}

		@Override
		protected void onPostExecute(ArrayList<AlBumObject> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			albumAdapter.resetData(result);
		}

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		// menu.setHeaderTitle("favorite");
		menu.add(0, v.getId(), 0, "Play All Album");
		menu.add(0, v.getId(), 0, "Rename Playlist");
		menu.add(0, v.getId(), 0, "Delete Playlist");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		if (item.getTitle() == "Play All Album") {

			AlBumObject albumObject = albumAdapter.getItem(info.position);
			int id_album = albumObject.get_id();
			listAlbumsong = audioUltility.getAllSongByAlbum(id_album);

			SongResource.listSong = listAlbumsong;

			albumAdapter.notifyDataSetChanged();
			MainActivity.mService.PlaySong(0);
			Intent intent = new Intent(getActivity(), Main_play.class);
			startActivity(intent);

		} else if (item.getTitle() == "Delete Playlist") {
			deleteplaylist();
		} else if (item.getTitle() == "Rename Playlist") {
			renameplaylist();
		} else {
			return false;
		}
		return true;

	}

	private void renameplaylist() {
		// TODO Auto-generated method stub

	}

	private void deleteplaylist() {
		// TODO Auto-generated method stub

	}

	private void playall() {
		// TODO Auto-generated method stub

	}

}